﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

namespace Lswweb.Domain
{
    public class WebRequest
    {
        private string url;
        private string args;
        private RequestMethod requestMethod = RequestMethod.Get;
        private WebProxy proxy;
        private Encoding encoding;

        public WebRequest(string requestUrl)
            : this(requestUrl, string.Empty) { }

        public WebRequest(string requestUrl, string args)
            : this(requestUrl, args, RequestMethod.Post,Encoding.Default) { }

        public WebRequest(string requestUrl, string args, RequestMethod requestMethod,Encoding encoding)
        {
            if (string.IsNullOrEmpty(requestUrl))
                throw new ArgumentNullException("requestUrl");

            this.url = requestUrl;
            this.args = args;
            this.requestMethod = requestMethod;
            this.encoding = encoding;
        }

        public string GetRequest()
        {
            string responseStr = string.Empty;

            WebResponse webResponse = GetResponse();
            using (StreamReader responseReader = new StreamReader(webResponse.GetResponseStream(), Encoding.Default))
            {
                responseStr = responseReader.ReadToEnd();
            }

            return responseStr;
        }

        public WebResponse GetResponse()
        {
            HttpWebRequest request = (HttpWebRequest)System.Net.WebRequest.Create(RequestUrl);
            request.Proxy = Proxy;
            request.Method = Method.ToString();
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = string.IsNullOrEmpty(Args) ? 0 : Args.Length;

            //将请求参数写入到RequestStream当中
            if (!string.IsNullOrEmpty(Args))
            {
                StreamWriter requestWriter = new StreamWriter(request.GetRequestStream(), Encoding.Default);
                requestWriter.Write(Args);
                requestWriter.Close();
            }

            return request.GetResponse();
        }

        public MatchCollection GetRequest(string regexPattern)
        {
            if (string.IsNullOrEmpty(regexPattern))
                throw new Exception("匹配的正则表达项不能为空，请配置参数regexPattern。");

            Regex regex = new Regex(regexPattern);
            return regex.Matches(GetRequest());
        }

        #region Public Properties

        public string RequestUrl
        {
            get { return url; }
            set { url = value; }
        }

        public string Args
        {
            get { return args; }
            set { args = value; }
        }

        public RequestMethod Method
        {
            get { return requestMethod; }
            set { requestMethod = value; }
        }

        public WebProxy Proxy
        {
            get { return proxy; }
            set { proxy = value; }
        }

        #endregion
    }

    public enum RequestMethod
    {
        Get,
        Post
    }
}
